#include "stdafx.h"
#include <afxinet.h>
#include "HTTPGet.h"
#include <iostream>
#include <iosfwd>
#include<fstream>
 

using namespace std;


bool CHttpGetFile::HttpGet(const string& str)
{
	unsigned int ret= 0;
	CInternetSession Session(NULL,0);
	ret = Session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,100000);
	string Str;
	CHttpFile* pHttpFile = NULL;
	try 
	{
		pHttpFile =(CHttpFile*) Session.OpenURL(CString(str.c_str()));
	}
	catch(CException * pExcept)
	{
		pExcept;
		return false;
	}
	char pFileChar[1024];
	memset(pFileChar,0,1024);
	int nSize;// = pHttpFile->Read(pFileChar,1024);
	
	
	ifstream fin;     
	//ofstream fout("C:nono.png",);
	ofstream fout("nono.bmp", ios::binary);


	CFile   m_File; 
	//m_File.Open( _T("c:\\test.jpg"),CFile::modeWrite|CFile::modeCreate|CFile::typeBinary); 
	m_File.Open(_T("test2.jpg"),CFile::modeWrite|CFile::modeCreate);//,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary); 
	nSize = pHttpFile->Read(pFileChar,1024);
	while(nSize)
	{
		//m_File.Write(pFileChar,nSize);
		fout.write(pFileChar,nSize);
		nSize = pHttpFile->Read(pFileChar,1024);
	}
	m_File.Close();
// 	m_File.Close();    
// 	myHttpFile-> Close()
// 	while(dwRead = myHttpFile->Read(szBuffer,1024))
// 	{
// 		m_File.Write(szBuffer,dwRead);
// 	}
// 
// 	fout.write(pFileChar,nSize);
	fout.close();
	//nSize = pHttpFile->GetLength();
	//file1.read("123",5);
	int a;
	a=0;
	return true;
}

// 
// std::string CHttpGetFile::GetString(int i)
// {
// 
// }